package com.cip.master.dao.impl;

import java.util.List;

import com.cip.admin.dao.impl.CipHibernateDAOSupport;
import com.cip.admin.model.User;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.cip.master.dao.BlogDAO;
import com.cip.master.model.Blog;
import com.cip.master.model.Comments;

public class BlogDAOImpl extends CipHibernateDAOSupport implements BlogDAO {
	
	public List<Blog> getMyBlogsById(int id) throws Exception{
		String query="select blog from Blog as blog where blog.user.id = '"+id+"'";
		List<Blog> blog = getCipHibernateTemplate().find(query);
		System.out.println("blog======" + blog);
		return blog;
	}
	
	public List<Blog> getOthersBlogList(int id) throws Exception{
		String query="select blog from Blog as blog where blog.user.id != '"+id+"'";
		List<Blog> blog = getCipHibernateTemplate().find(query);
		return blog;
	}
	
	public Blog saveOrUpdateBlog(Blog blog) throws Exception {
		getHibernateTemplate().saveOrUpdate(blog);
		return blog;
	}
	
	public Blog getBlogDetails(int id) throws Exception {
		System.out.println("blog DAO======= idd=====" + id);
		String query = "select blog from Blog as blog where blog.id = '"+id+"'";
		List<Blog> blog = getCipHibernateTemplate().find(query);
		return blog.get(0);
	}
	
	public List<Comments> getCommentsByBlogId(int blogId) throws Exception{
		System.out.println("in comment listdao======" + blogId);
		String query = "select comments from Comments as comments where comment.blog = '"+blogId+"' ";
		List<Comments> comments = getCipHibernateTemplate().find(query);
		return comments;
	}
	
	public Comments saveOrUpdateComment(Comments comments) throws Exception {
		System.out.println("Comments sve============blogdao========");
		getHibernateTemplate().saveOrUpdate(comments);
		return comments;
	}
}
